﻿using Code;
using HeerCards;
using Panuon.UI.Silver;
using PropertyChanged;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Forms;

namespace github.heerheer.card2.UI.Pages
{
    /// <summary>
    /// CardSettings.xaml 的交互逻辑
    /// </summary>
    public partial class CardSettings : Window
    {
        public HhhCard Card { get; set; }

        public CardSettings(HCCard card)
        {
            InitializeComponent();
            Card = new HhhCard()
            {
                Name = card.Name,
                Description = card.Description,
                GUID = card.GUID,
                ExtraProbability = card.ExtraProbability,
                LocalImagePath = card.LocalImagePath
            };
            StackP.DataContext = Card;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (Card.Name == null)
            {
                System.Windows.MessageBox.Show("参数不全");
                return;
            }
            if (Card.Name.Contains("崩坏"))
            {
                //触发提示
                //https://cqp.cc/t/47221
                var r = MessageBoxX.Show("我们推荐崩坏三卡池使用另一个插件\n点击确定跳转", "跳转",null,MessageBoxButton.YesNo);
                if (r == MessageBoxResult.Yes)
                {
                    Process.Start(@"https://cqp.cc/t/47221");
                }
            }
            this.DialogResult = true;
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            ObservableCollection<string> obv = new ObservableCollection<string>();
            List<string> list = new List<string>(obv);
            obv = new ObservableCollection<string>(list);
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            //查看路径如何填写
            var content =
                "<h1 style=\"font - size: 32px; font - weight: bold; border - bottom: 2px solid rgb(204, 204, 204); padding: 0px 4px 0px 0px; text - align: left; margin: 0px 0px 10px; \">卡片的图片路径</h1>" +
                "<h2>必看</h2><p><span style=\"font - size: 18px; color: rgb(255, 0, 0); \">图片发送：全局开关&gt;卡池开关&gt;等级开关</span> " +
                "<span style=\"color: rgb(165, 165, 165); \">（全局开关默认关,卡池\\等级开关默认开）</span></p>" +
                "<p><span style=\"color: rgb(165, 165, 165); \"></span></p><p><span style=\"color: rgb(165, 165, 165); \"></span></p><hr/>" +
                "<p>本插件所有图片必须<strong>全部</strong>存放于：<strong>酷Q Pro\\data\\image\\card2</strong>文件夹下面</p>" +
                "<p>比如我想要给我的卡创建一个<em>cxk.png</em></p><p>那么我需要进入<strong>\\data\\image\\card2\\</strong> 把图片放进去</p><p>然后卡片路径填写 <em>cxk.png</em></p>" +
                "<p>同样的，我们可以稍加整理，在card2文件夹下面创建kunkun文件夹，再把<em style=\"white - space: normal; \">cxk.png放进去</em></p>" +
                "<p>图片路径那里就填写<em>kunkun\\cxk.png</em></p>";
            Infos.NewsInfo info = new Infos.NewsInfo() { Message = content, Title = "图片路径帮助" };
            NoticeView w = new NoticeView(info);
            w.ShowDialog();
        }

        private void Button_Click_Find(object sender, RoutedEventArgs e)
        {
            var bpath = AppMain.Common.AppDirectory ?? "";
            var imagepath = bpath.Replace("app\\github.heerheer.card2", "image\\card2");
            OpenFileDialog openFileDialog = new OpenFileDialog();
            /*
             * 如果已经存在 card2\poolname\cardname的文件，会自动打开这个目录
             * 不存在会打开桌面！
             * */
            if (Directory.Exists(imagepath))
            {
                openFileDialog.InitialDirectory = imagepath;
            }
            else
            {
                openFileDialog.InitialDirectory = AppMain.Common.AppDirectory;
            }
            var r = openFileDialog.ShowDialog();
            if (r == System.Windows.Forms.DialogResult.OK)
            {
                //cccc\\data\\image\\card2\\
                var filepath = openFileDialog.FileName;
                if (filepath.StartsWith(imagepath))
                {
                    //在card2文件夹下的文件，不需要复制
                    Card.LocalImagePath = imagepath != "" ? filepath.Replace(imagepath, "") : new FileInfo(filepath).Name;
                }
                else
                {
                    //需要复制进来
                    File.Copy(filepath, imagepath + new FileInfo(filepath).Name);
                }
            }
        }
    }

    [AddINotifyPropertyChangedInterface]
    public class HhhCard : HCCard, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public string LocalImagePath { get; set; }

        private void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}